#include "StartGameScene.h"
#include "MainGameScene.h"
#include "GameUpgradeScene.h"
#include "SceneLevelSelect.h"

using namespace cocos2d;
using namespace cocos2d::extension;


CCScene* StartGameScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    StartGameScene *layer = StartGameScene::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool StartGameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    _winSize = CCDirector::sharedDirector()->getWinSize();
	Setting* setting = GameUtil::instance()->getSetting();
	
	//CCNode *pNode = CCSSceneReader::sharedSceneReader()->createNodeWithSceneFile("SceneProject.json");
	m_pUiLayer = UILayer::create();
    addChild(m_pUiLayer);
        
    m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_01_1.json"));
    m_pUiLayer->addWidget(m_pWidget);
	
	// panel
	UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
	root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
	root->setAnchorPoint(ccp(0.5f, 0.5f));

	// buttons
	UITextButton *button_01 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button_01"));
    button_01->addReleaseEvent(this, coco_releaseselector(StartGameScene::startGameCallback));
	button_01->setText(setting->getStr("start game"));

    UITextButton *button_02 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button_02"));
    button_02->addReleaseEvent(this, coco_releaseselector(StartGameScene::challengeCallback));
        
    UITextButton *button_03 = dynamic_cast<UITextButton*>(m_pUiLayer->getWidgetByName("button_03"));
    button_03->addReleaseEvent(this, coco_releaseselector(StartGameScene::achieveCallback));

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/start.mp3", true);

	return true;
}

void StartGameScene::startGameCallback(CCObject* sender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
    CCDirector::sharedDirector()->replaceScene(MainGameScene::scene());
}

void StartGameScene::challengeCallback(CCObject* sender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
    CCDirector::sharedDirector()->replaceScene(GameUpgradeScene::scene());
}

void StartGameScene::achieveCallback(CCObject* sender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->end();
    CCDirector::sharedDirector()->replaceScene(SceneLevelSelect::scene());
    //CCDirector::sharedDirector()->replaceScene(UISceneManager::sharedUISceneManager()->nextUIScene());
}